<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: notify_credit.php 22319 2011-04-29 09:40:43Z monkey $
 */

define('IN_API', true);
define('CURSCRIPT', 'api');

require '../../common.php';
$apitype = empty($_GET['attach']) || !preg_match('/^[a-z0-9]+$/i', $_GET['attach']) ? 'alipay' : $_GET['attach'];
require_once B_ROOT.'./api/trade/api_'.$apitype.'.php';
$notifydata = trade_notifycheck('credit');

$PHP_SELF = $_SERVER['PHP_SELF'];
$_G['siteurl'] = htmlspecialchars('http://'.$_SERVER['HTTP_HOST'].preg_replace("/\/+(api\/trade)?\/*$/i", '', substr($PHP_SELF, 0, strrpos($PHP_SELF, '/'))).'/');
if($notifydata['validator']) {

	$orderid = $notifydata['order_no'];
	$postprice = $notifydata['price'];
	$order = DB::fetch_first("SELECT o.* FROM ".DB::table('goodjoin')." o  WHERE o.orderid='$orderid'");
	if($order && floatval($postprice) == floatval($order['price']) && ($apitype == 'tenpay' || $_G['setting']['ec_account'] == $_REQUEST['seller_email'])) {
		if($order['status'] == 0) {
			DB::query("UPDATE ".DB::table('goodjoin')." SET status='1', paydate='$_G[timestamp]', trade_no='$notifydata[trade_no]\t$apitype' WHERE orderid='$orderid'");
			uc_pm_send(0, $_G['uid'] , $lang['reserve_pm_title'] , $lang['pm_content'].$notifydata['trade_no']);
		}
	}
}

if($notifydata['location']) {
	if($apitype == 'tenpay') {
		echo <<<EOS
<meta name="TENCENT_ONLINE_PAYMENT" content="China TENCENT">
<html>
<body>
<script language="javascript" type="text/javascript">
window.location.href='$_G[siteurl]store.php?id=$order[shopid]&action=good&do=paysucceed';
</script>
</body>
</html>
EOS;
	} else {
		header('location: '.$_G['siteurl'].'store.php?id='.$order['shopid'].'&action=good&do=paysucceed');
	}
} else {
	exit($notifydata['notify']);
}

?>